package com.fsc.controller;

import com.fsc.entity.ResultWrapper;
import com.fsc.entity.domain.ParentInfo;
import com.fsc.entity.domain.StudentInfo;
import com.fsc.entity.domain.TeacherInfo;
import com.fsc.entity.dto.ParentDTO;
import com.fsc.entity.dto.StudentDTO;
import com.fsc.entity.dto.TeacherDTO;
import com.fsc.service.ParentInfoService;
import com.fsc.service.StudentInfoService;
import com.fsc.service.TeacherInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.fsc._enum.ReturnEnum.SUCCESS;

@RestController
@RequestMapping("/perfect/")
public class UserInfoController {

    @Autowired
    private TeacherInfoService teacherInfoService;
    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private ParentInfoService parentInfoService;

    @PostMapping("teacher")
    public ResultWrapper<String> add(@RequestBody TeacherDTO teacherDTO){
        TeacherInfo teacher = teacherInfoService.select(teacherDTO.getTeacherId());
        if(teacher != null){
            teacherInfoService.update(teacherDTO);
            return ResultWrapper.success(SUCCESS);
        }
        TeacherInfo teacherInfo = TeacherInfo.builder()
                .teacherId(teacherDTO.getTeacherId())
                .gender(teacherDTO.getGender())
                .teacherName(teacherDTO.getTeacherName())
                .subject(teacherDTO.getSubject())
                .build();
        teacherInfoService.save(teacherInfo);
        return ResultWrapper.success(SUCCESS);
    }

    @PostMapping("student")
    public ResultWrapper<String> add(@RequestBody StudentDTO studentDTO){
        StudentInfo student = studentInfoService.select(studentDTO.getStudentId());
        if(student != null){
            studentInfoService.update(studentDTO);
            return ResultWrapper.success(SUCCESS);
        }
        StudentInfo studentInfo = StudentInfo.builder()
                .studentId(studentDTO.getStudentId())
                .studentName(studentDTO.getStudentName())
                .gender(studentDTO.getGender())
                .build();
        studentInfoService.save(studentInfo);
        return ResultWrapper.success(SUCCESS);
    }

    @PostMapping("parent")
    public ResultWrapper<String> add(@RequestBody ParentDTO parentDTO){
        ParentInfo parent = parentInfoService.select(parentDTO.getParentId());
        if(parent != null){
            parentInfoService.update(parentDTO);
            return ResultWrapper.success(SUCCESS);
        }
        ParentInfo parentInfo = ParentInfo.builder()
                .parentId(parentDTO.getParentId())
                .parentName(parentDTO.getParentName())
                .gender(parentDTO.getGender())
                .build();
        parentInfoService.save(parentInfo);
        return ResultWrapper.success(SUCCESS);
    }
}
